package com.enid.rongtalkdemo;

import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.Random;
import java.util.StringJoiner;

import cn.rongcloud.im.SealAppContext;
import cn.rongcloud.im.SealUserInfoManager;
import cn.rongcloud.im.server.utils.NLog;
import io.rong.imkit.RongIM;
import io.rong.imlib.RongIMClient;
import io.rong.imlib.model.UserInfo;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class LoginActivity extends AppCompatActivity {
    private EditText userIdText;
    private String userPhoto1 = "http://img0.imgtn.bdimg.com/it/u=737277182,2730582748&fm=26&gp=0.jpg";
    private String userPhoto2 = "http://img1.imgtn.bdimg.com/it/u=2561011807,290788206&fm=26&gp=0.jpg";
    private String userPhoto = "";
//    private String test1Token = "DCjmQjHHM2rzxZL8CcNg2277uPnsmQMGGnYPWSr3Z2sMNN1BYaspCf4KlNud028ua5xEzdAtQj42lu5puVqUhA==";
//    private String test2Token = "gBx8pGtBB5mqcMclLJbpiQKUfvEtVwUqQ5/khPsdQetKoluW+BlX/BPmug3W2JceLydj9tPQEOlx7I/kqn934w==";
    //新的
    private String test1Token = "PRq+0aiB01tYlHVMnYK39k0YjmLL/120miCIy6hRjU0XKCMJPhpglrGUGEXzI9DJemgIU9WbgrfvxPXLtyET/A==";
    private String test2Token = "r2La29y4tmVagbixpT8/OyHfVFEFwevba/R5QGpzeqQ7YspF3oi9/KR6rwQhEjylvtEp4h2Gjnh3aFd4ywH0Kg==";
    private String userName1 = "测试用户1";
    private String userName2 = "测试用户2";
    private String userName = "";
    private String token = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        userIdText = findViewById(R.id.userIdText);
        userIdText.setText("test1");

        String cacheToken = PreferenceUtil.getString(this, ConstantUtil.RONGYUN_USER_TOKEN);
        if (!TextUtils.isEmpty(cacheToken)) {
            RongIM.connect(cacheToken, new RongIMClient.ConnectCallback() {
                @Override
                public void onTokenIncorrect() {

                }

                @Override
                public void onSuccess(String s) {
                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                    startActivity(intent);
                    finish();
                }

                @Override
                public void onError(RongIMClient.ErrorCode errorCode) {

                }
            });
        }
    }

    public void onLogin(View view) {
        getToken();
    }

    private void getToken() {
        final String userId = userIdText.getText().toString();
        if (userId.equals("test1")) {
            token = test1Token;
            userPhoto = userPhoto1;
            userName = userName1;
        } else if (userId.equals("test2")) {
            token = test2Token;
            userPhoto = userPhoto2;
            userName = userName2;
        }

        RongIM.connect(token, new RongIMClient.ConnectCallback() {
            @Override
            public void onTokenIncorrect() {
                NLog.d("enid", "onTokenIncorrect");
            }

            @Override
            public void onSuccess(String s) {
                NLog.d("enid", "onSuccess");
                PreferenceUtil.putString(LoginActivity.this, ConstantUtil.USER_ID, userId);
                PreferenceUtil.putString(LoginActivity.this, ConstantUtil.USER_NAME, userName);
                PreferenceUtil.putString(LoginActivity.this, ConstantUtil.RONGYUN_USER_TOKEN, token);//存入sp
                SealUserInfoManager.getInstance().openDB();
                //设置用户头像
                RongIM.getInstance().setCurrentUserInfo(new UserInfo(userId, userName, Uri.parse(userPhoto)));
//                RongIM.getInstance().setMessageAttachedUserInfo(true);
                startActivity(new Intent(LoginActivity.this, MainActivity.class).putExtra("loginUser",userId));
            }

            @Override
            public void onError(RongIMClient.ErrorCode errorCode) {
                NLog.d("enid", "onError--errorCode=" + errorCode);
            }
        });

    }
}
